<template>
	<view class="main">
		<view class="header">
			<view class="search">
				<u-icon name="search" color="#999999" size="28"></u-icon>
				<input type="text" v-model="data.keyword" placeholder="请搜索你需要的商品" />
				<view class="btn" @tap="search">
					搜索
				</view>
			</view>
		</view>
		<view class="tab">
			<u-tabs :list="classList" @click="click" keyName="title" :activeStyle="{
			        color: '#EA504F',
			        fontWeight: 'bold',
			    }" :inactiveStyle="{
			        color: '#333',
			    }" lineColor="#EA504F"></u-tabs>
		</view>
		<view v-if="tab2" class="tab2">
			<scroll-view class="scroll1" scroll-x="true" enable-flex style="flex-direction: row;">
				<view @tap="click2(item)" :class="{item:true,active:seleTab == item.id}"
					v-for="(item,index) in classList2" :key="index">
					{{item.title}}
				</view>
			</scroll-view>
		</view>
		<u-empty v-if="list.length == 0" mode="list" text="暂无商品" textColor="#fff" iconColor="#fff"></u-empty>
		<view class="list">
			<view class="item" v-for="(item, index) in list" :key="index"
				@tap="routerTo(`/pageB/goodDetail?id=${item.id}`)">
				<view class="top">
					<!-- #ifdef MP-WEIXIN -->
					<image lazy-load :src="item.image" mode=""></image>
					<!-- #endif -->
					<!-- #ifndef MP-WEIXIN -->
					<easy-loadimage class="image" :scroll-top="scrollTop" :image-src="item.image"></easy-loadimage>
					<!-- #endif -->
				</view>
				<view class="bottom">
					<view class="a">
						{{item.name}}
					</view>
					<view class="b">
						￥{{item.member_price}} <text
							style="font-size: 12px;text-decoration: line-through;margin-left: 5px;">￥{{item.original_price}}</text>
					</view>
					<!-- <view class="c">
						<text>赠送：{{item.give_scores}}积分</text>
						<text>赠送：{{item.give_shops}}贡献值</text>
					</view> -->
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		cates,
		list
	} from '@/api/index.js'
	export default {
		data() {
			return {
				seleTab: 0,
				list: [],
				classList: [],
				data: {
					page_size: 10,
					category_id: '',
					page: 1,
					keyword: ''
				},
				isShow: false,
				last_page: '',
				classList2: [],
				tab2: true,
				scrollTop: 0
			};
		},
		onPageScroll({
			scrollTop
		}) {
			// 传入scrollTop值并触发所有easy-loadimage组件下的滚动监听事件
			this.scrollTop = scrollTop;
		},
		onLoad() {
			this.getList()
		},
		onReachBottom() {
			if (this.data.page == this.last_page || this.last_page == 0) {
				uni.showToast({
					title: '已经是最后一页了',
					icon: 'none'
				})
			} else {
				this.data.page++
				this.getGoods()
			}
		},
		methods: {
			search() {
				this.data.page = 1
				this.getGoods()
			},
			seleClass(id) {

			},
			click2(e) {
				this.seleTab = e.id
				this.data.keyword = ''
				this.data.page = 1
				this.data.category_id = e.id
				this.getGoods()
			},
			click(e) {
				this.tab2 = false
				this.data.keyword = ''
				this.seleTab = e.id
				this.data.category_id = e.id
				this.data.page = 1
				this.classList2 = e.children
				this.tab2 = true
				this.getGoods()
			},
			async getList() {
				let res = await cates()
				this.seleTab = res.data[0].id
				this.data.category_id = this.seleTab
				res.data.forEach((item, index) => {
					item.children.unshift({
						id: item.id,
						title: '全部'
					})
				})
				this.classList = res.data
				this.classList2 = res.data[0].children
				this.getGoods()
			},
			async getGoods() {
				if (this.data.page == 1) {
					this.list = []
				}
				let res = await list(this.data)
				if (res.code == 1) {
					this.last_page = res.data.last_page
					res.data.data.forEach(item => {
						this.list.push(item)
					})
				}
			},
			routerTo(url) {
				uni.navigateTo({
					url: url
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.main {
		min-height: 100vh;
		display: flex;
		flex-direction: column;
		background-color: #f6f6f6;

		.header {
			padding: 180rpx 30rpx 0rpx;
			background-color: #fff;
			background-color: #f6f6f6;
			// background: linear-gradient(180deg, #0A84FF 0%, #1D242C 100%);
			// box-shadow: 0rpx 2rpx 12rpx 0rpx rgba(0, 0, 0, 0.05);
			border-radius: 0rpx 0rpx 40rpx 40rpx;
			margin-bottom: 20rpx;

			.search {
				border-radius: 200rpx;
				background-color: #fff;
				display: flex;
				align-items: center;
				padding: 20rpx 24rpx;

				image {
					width: 24rpx;
					height: 24rpx;
					margin-right: 14rpx;
				}

				input {
					flex: 1;
					color: #000;
				}

				.btn {
					padding: 6rpx 20rpx;
					background-color: #EA504F;
					font-size: 24rpx;
					border-radius: 200rpx;
					color: #fff;
				}
			}
		}

		.tab {
			background-color: #fff;
			color: #000;
			padding: 0 30rpx;
			margin-bottom: 3rpx;
		}

		.tab2 {
			padding: 24rpx 40rpx;
			background-color: #fff;

			.scroll1 {
				display: flex;
				overflow: hidden;
				white-space: nowrap;

				::v-deep.uni-scroll-view-content {
					display: flex;
					flex-wrap: nowrap;

					.item {
						margin: 0 15rpx;
						color: #000;
						font-size: 28rpx;
					}

					.active {
						color: #EA504F;
					}
				}

				.item {
					margin: 0 10rpx;
					color: #333;
					font-size: 28rpx;
				}

				.active {
					color: #EA504F;
				}
			}
		}

		.list {
			padding: 30rpx;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;

			.item {
				width: 48%;
				background-color: #fff;
				margin-bottom: 20rpx;
				border-radius: 15rpx;

				.top {
					width: 100%;

					.image {
						width: 100%;
						height: 240rpx;

						::v-deep image {
							border-radius: 15rpx 15rpx 0 0;
						}
					}

					image {
						width: 100%;
						height: 240rpx;
						border-radius: 15rpx 15rpx 0 0;
					}
				}

				.bottom {
					padding: 20rpx;
					color: #000;

					.a {
						font-size: 26rpx;
						font-weight: 700;
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis;
						margin-bottom: 24rpx;
					}

					.b {
						font-size: 30rpx;
						color: #EA504F;
					}
					
					.c {
						display: flex;
						flex-direction: column;
						font-size: 24rpx;
						color: #FC2F27;
					}
				}
			}

		}

		.con {
			flex: 1;
			width: 100%;
			display: flex;

			.left {
				width: 25%;
				background-color: #EA504F;

				.list {
					width: 100%;
					height: 100%;

					li {
						padding: 20rpx 0 20rpx 10rpx;
						display: flex;
						align-items: center;

						.hr {
							height: 40rpx;
							width: 8rpx;
							border-radius: 200rpx;
							margin-right: 20rpx;
						}

						.msg {
							color: #232323;
						}
					}

					.active {
						background-color: #1D242C;

						.hr {
							background-color: #005D3D;
						}

						.msg {
							font-weight: 700;
							color: #fff;
						}
					}

					.next {
						border-radius: 0 38rpx 0 0;
					}

					.prve {
						border-radius: 0 0 38rpx 0;
					}
				}
			}

			.right {
				width: 75%;
				padding: 20rpx 30rpx 0 30rpx;
				max-height: 100%;

				.list {
					max-height: 100%;
					padding-bottom: 40rpx;
				}

			}
		}
	}
</style>